package com.fengwk.deeplearning.core;

import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.ops.transforms.Transforms;

/**
 * ReLU运算单元
 * 
 * @author fengwk
 *
 */
class ReLUUnitCompute implements UnitCompute {
	
	private static final long serialVersionUID = -4014923229037173084L;
	
	@Override
	public INDArray activate(INDArray Z) {
		return Transforms.relu(Z);
	}

	@Override
	public INDArray dActivate(INDArray Z) {
		INDArray buf = Nd4j.zeros(Z.shape());
		for (int i = 0; i < buf.length(); i ++) {
			if (Z.getFloat(i) >= 0)
				buf.putScalar(i, 1);
		}
		return buf;
	}
	
}
